package com.ydzt.fanka.server.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;


public class ConfigFileUtil {
	static Logger logger = Logger.getLogger(ConfigFileUtil.class.getName());
	public static String GetValueByKey(String key){
		String value = null;
		//String XmlPath = this.getClass().getResource("").getPath() + "SYSCONFIG.xml";
		
		String XmlPath = System.getProperty("user.dir") + "/SYSCONFIG.xml";
		logger.info("读取配置文件    路径为：" + XmlPath);
		Document doc = XmlUtil.ReadXml(XmlPath);
		List<Element> nodes = new ArrayList<Element>();
		nodes = XmlUtil.GetNodes(doc);
		for(int i = 0;i < nodes.size();i++){
			Element elm = nodes.get(i);
			if(key.equals(elm.getName())){
				value = elm.attributeValue("VALUE");
			}
		}
		return value;
	}
	
	
	public static String GetValueByKey(String key,String xmlpath){
		String value = null;
		//String XmlPath = this.getClass().getResource("").getPath() + "SYSCONFIG.xml";
		String XmlPath = xmlpath;
		Document doc = XmlUtil.ReadXml(XmlPath);
		List<Element> nodes = new ArrayList<Element>();
		nodes = XmlUtil.GetNodes(doc);
		for(int i = 0;i < nodes.size();i++){
			Element elm = nodes.get(i);
			if(key.equals(elm.getName())){
				value = elm.attributeValue("VALUE");
			}
		}
		return value;
	}
	
	public static String getTableName(String key){
		
		String tableName = GetValueByKey(key,System.getProperty("user.dir") + "/param_table_mapping.xml");
		return tableName;
	}
	
	public static int getFlagWithBroadCast(String key){
		return Integer.parseInt(GetValueByKey(key,System.getProperty("user.dir") + "/broadcastlist.xml"));
	}
	
	public static int getOpenPort()
	{
		String strPort = GetValueByKey("OPENPORT");
		return Integer.parseInt(strPort);
	}
}
